#include "qwebstation.h"

#include <iostream>

QWebStation::QWebStation(QWidget *parent) :
	QMainWindow(parent),
	settings(new QSettings(QLatin1String("Rex"),QLatin1String("qwebstation"), this)),
	addRadioDialog(0)
{
	// create the process object for listen the radio
	program = "mplayer";
	mplayer_process = new QProcess(this);

	centralSpace = new QWSCentral(this);

	setCentralWidget(centralSpace);

	setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
	setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
	setDockOptions(QMainWindow::VerticalTabs);

	createActions();
	createQWSMenuBar();
	createQWSToolBar();
	createDockQWSControl();
}

void QWebStation::createActions() {
	newAct = new QAction(QIcon(":/resources/icons/add.png"),
			tr("&New station"), this);
	quitAct = new QAction(QIcon(":/resources/icons/quit.png"), tr("&Quit"),
			this);

	newAct->setStatusTip(tr("Add a new Station"));
	quitAct->setStatusTip(tr("Close the application"));

	connect(newAct, SIGNAL(triggered()), this, SLOT(newStation()));
	connect(quitAct, SIGNAL(triggered()), this, SLOT(close()));

	connect(centralSpace, SIGNAL(radioSel(RadioInfo)), this, SLOT(radioSel(RadioInfo)));
}

void QWebStation::createQWSMenuBar() {
	menu = menuBar()->addMenu(tr("&Station"));
	menu->addAction(newAct);

}

void QWebStation::createQWSToolBar() {
	stationToolBar = addToolBar(tr("&Station"));
	stationToolBar->addAction(newAct);
	stationToolBar->addAction(quitAct);

	stationToolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
	addToolBar(Qt::TopToolBarArea, stationToolBar);
}

void QWebStation::createDockQWSControl() {
	dockQWSControl = new QDockWidget(this);
	dockQWSControl->setObjectName(QLatin1String("dockQWSControl"));
	dockQWSControl->setAllowedAreas(Qt::BottomDockWidgetArea);
	dockQWSControl->setFeatures(QDockWidget::DockWidgetFloatable
			| QDockWidget::DockWidgetMovable);

	controlDock = new QWSControlDock(this);

	dockQWSControl->setWidget(controlDock);
	addDockWidget(Qt::BottomDockWidgetArea, dockQWSControl);

	connect(controlDock, SIGNAL(playStation()), this, SLOT(playStation()));
	connect(controlDock, SIGNAL(pauseStation()), this, SLOT(pauseStation()));
	connect(controlDock, SIGNAL(stopStation()), this, SLOT(stopStation()));
}

void QWebStation::radioSel(RadioInfo radio_sel) {
	radio = radio_sel;
}

void QWebStation::newStation() {
	if (!addRadioDialog) {
		addRadioDialog = new QWSAddRadio(this);
	}

	switch(addRadioDialog->isHidden()) {
	case 1:
		addRadioDialog->show();
		break;
	default:
		addRadioDialog->activateWindow();
		break;
	}

	connect(addRadioDialog, SIGNAL(finished(int)), this, SLOT(refreshStations()));
}

void QWebStation::playStation() {
	QStringList arguments;
	arguments << radio.getUrl();

	if (mplayer_process->state() == QProcess::Running || QProcess::Starting) {
		mplayer_process->close();
	}

	mplayer_process->start(program, arguments);
}

void QWebStation::pauseStation() {
	if (mplayer_process->state() == QProcess::Running || QProcess::Starting) {
		mplayer_process->close();
	}
}

void QWebStation::stopStation() {
	if (mplayer_process->state() == QProcess::Running || QProcess::Starting) {
		mplayer_process->close();
	}
}

QWebStation::~QWebStation() {

}
